/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DispositivoRadio.java
 *
 * Created on 7/07/2012, 07:35:09 PM
 */
package GUI;

import java.util.ArrayList;
import javax.swing.JButton;
import radio.Radio;

/**
 * DispositivoRadio.java
 * @author Paulo Chang-11076,Andrea Mendoza-11331,Vinicio Gomez-11120,Felipe Zelada-08164
 * GUI para una radio.
 * 09/07/2012
 */
public class DispositivoRadio extends javax.swing.JFrame {
    /**	 */
	private static final long serialVersionUID = 1L;
	/**instancia de un radio*/
	Radio MyRadio;
	/**Botones de la GUI*/
    ArrayList<JButton> MyButtons = new ArrayList<JButton>();
    /** Creates new form DispositivoRadio */
    public DispositivoRadio() {
        initComponents();        
        MyButtons.add(Btn01);
        MyButtons.add(Btn02);
        MyButtons.add(Btn03);
        MyButtons.add(Btn04);
        MyButtons.add(Btn05);
        MyButtons.add(Btn06);
        MyButtons.add(Btn07);
        MyButtons.add(Btn08);
        MyButtons.add(Btn09);
        MyButtons.add(Btn10);
        MyButtons.add(Btn11);
        MyButtons.add(Btn12);
        MyRadio = new Radio();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        FreqSaveCB = new javax.swing.JComboBox();
        SaveBtn = new javax.swing.JButton();
        FreqLbl = new javax.swing.JLabel();
        Btn01 = new javax.swing.JButton();
        Btn02 = new javax.swing.JButton();
        Btn03 = new javax.swing.JButton();
        Btn04 = new javax.swing.JButton();
        Btn05 = new javax.swing.JButton();
        Btn06 = new javax.swing.JButton();
        Btn07 = new javax.swing.JButton();
        Btn08 = new javax.swing.JButton();
        Btn09 = new javax.swing.JButton();
        Btn10 = new javax.swing.JButton();
        Btn11 = new javax.swing.JButton();
        Btn12 = new javax.swing.JButton();
        ChangeSignalBtn = new javax.swing.JToggleButton();
        PowerBtn = new javax.swing.JButton();
        UpFreqBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        FreqSaveCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        SaveBtn.setFont(new java.awt.Font("Tahoma", 0, 8));
        SaveBtn.setText("SAVE");
        SaveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveBtnActionPerformed(evt);
            }
        });

        FreqLbl.setText("                  ");
        FreqLbl.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        Btn01.setText("01");
        Btn01.setToolTipText("");
        Btn01.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClk(evt);
            }
        });

        Btn02.setText("02");
        Btn02.setToolTipText("");
        Btn02.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClk(evt);
            }
        });

        Btn03.setText("03");
        Btn03.setToolTipText("");
        Btn03.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClk(evt);
            }
        });

        Btn04.setText("04");
        Btn04.setToolTipText("");
        Btn04.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClk(evt);
            }
        });

        Btn05.setText("05");
        Btn05.setToolTipText("");
        Btn05.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClk(evt);
            }
        });

        Btn06.setText("06");
        Btn06.setToolTipText("");
        Btn06.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClk(evt);
            }
        });

        Btn07.setText("07");
        Btn07.setToolTipText("");
        Btn07.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClk(evt);
            }
        });

        Btn08.setText("08");
        Btn08.setToolTipText("");
        Btn08.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClk(evt);
            }
        });

        Btn09.setText("09");
        Btn09.setToolTipText("");
        Btn09.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClk(evt);
            }
        });

        Btn10.setText("10");
        Btn10.setToolTipText("");
        Btn10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClk(evt);
            }
        });

        Btn11.setText("11");
        Btn11.setToolTipText("");
        Btn11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClk(evt);
            }
        });

        Btn12.setText("12");
        Btn12.setToolTipText("");
        Btn12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClk(evt);
            }
        });

        ChangeSignalBtn.setText("FM");
        ChangeSignalBtn.setToolTipText("");
        ChangeSignalBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChangeSignalBtnActionPerformed(evt);
            }
        });

        PowerBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/botonOFF.png"))); // NOI18N
        PowerBtn.setBorder(null);
        PowerBtn.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/botonOFFselected.png"))); // NOI18N
        PowerBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PowerBtnActionPerformed(evt);
            }
        });

        UpFreqBtn.setToolTipText("");
        UpFreqBtn.setLabel("˄");
        UpFreqBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UpFreqBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(FreqSaveCB, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(SaveBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Btn01, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(Btn07, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Btn02, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(Btn08, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(FreqLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(UpFreqBtn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Btn03, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Btn09, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Btn04, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(Btn10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Btn05, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(Btn11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Btn06, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(Btn12))
                        .addGap(6, 6, 6))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ChangeSignalBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(PowerBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(PowerBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(FreqLbl)
                            .addComponent(UpFreqBtn)
                            .addComponent(ChangeSignalBtn))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(FreqSaveCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(Btn01)
                                    .addComponent(Btn02)
                                    .addComponent(Btn03)
                                    .addComponent(Btn04))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(SaveBtn)
                                    .addComponent(Btn07)
                                    .addComponent(Btn08)
                                    .addComponent(Btn09)
                                    .addComponent(Btn10)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(Btn05)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Btn11))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(Btn06)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Btn12)))))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void ChangeSignalBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChangeSignalBtnActionPerformed
// TODO add your handling code here:    
    if (MyRadio.IsOn())
        MyRadio.ChangeSignal();
    RefreshGui();
}//GEN-LAST:event_ChangeSignalBtnActionPerformed

private void UpFreqBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UpFreqBtnActionPerformed
// TODO add your handling code here:    
    if (MyRadio.IsOn())
        MyRadio.NextFreq();
    RefreshGui();
}//GEN-LAST:event_UpFreqBtnActionPerformed

private void PowerBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PowerBtnActionPerformed
// TODO add your handling code here:
    MyRadio.Power();
    RefreshGui();
}//GEN-LAST:event_PowerBtnActionPerformed

private void SaveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveBtnActionPerformed
// TODO add your handling code here:
    if (MyRadio.IsOn()) {
        int index = Integer.valueOf(FreqSaveCB.getSelectedItem().toString());
        MyRadio.SaveFreq(index);
    }
}//GEN-LAST:event_SaveBtnActionPerformed

private void buttonClk(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClk
// TODO add your handling code here:
    if (MyRadio.IsOn())
        MyRadio.ChangeTune(MyButtons.indexOf(evt.getSource())+1);
    RefreshGui();
}//GEN-LAST:event_buttonClk

private void RefreshGui() {
    if (MyRadio.IsOn()) {
        FreqLbl.setText(String.valueOf(MyRadio.GetFreq()));
        ChangeSignalBtn.setText(MyRadio.IsAm()?"AM":"FM");
        PowerBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/botonON.png")));
        PowerBtn.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/botonONselected.png"))); // NOI18N
        
    } else {
         FreqLbl.setText(" ");
         PowerBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/botonOFF.png"))); // NOI18N
         PowerBtn.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/botonOFFselected.png"))); // NOI18N
    }
}
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DispositivoRadio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DispositivoRadio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DispositivoRadio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DispositivoRadio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DispositivoRadio().setVisible(true);                
            }            
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Btn01;
    private javax.swing.JButton Btn02;
    private javax.swing.JButton Btn03;
    private javax.swing.JButton Btn04;
    private javax.swing.JButton Btn05;
    private javax.swing.JButton Btn06;
    private javax.swing.JButton Btn07;
    private javax.swing.JButton Btn08;
    private javax.swing.JButton Btn09;
    private javax.swing.JButton Btn10;
    private javax.swing.JButton Btn11;
    private javax.swing.JButton Btn12;
    private javax.swing.JToggleButton ChangeSignalBtn;
    private javax.swing.JLabel FreqLbl;
    private javax.swing.JComboBox FreqSaveCB;
    private javax.swing.JButton PowerBtn;
    private javax.swing.JButton SaveBtn;
    private javax.swing.JButton UpFreqBtn;
    private javax.swing.ButtonGroup buttonGroup1;
    // End of variables declaration//GEN-END:variables
}
